package com.pfizer.PicView;

import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;  
import java.lang.ref.SoftReference;  
import java.net.HttpURLConnection;  
import java.net.URL;  
import java.util.HashMap;  
import android.content.Context;  
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
 import android.os.AsyncTask;  
import android.view.View;  
import android.webkit.URLUtil;  
import android.widget.ImageView;


public class AsyncViewTask extends AsyncTask<View, Void, Bitmap>
{  

	private ImageView mView;  

	private HashMap<String, SoftReference<Bitmap>> imageCache;  
	Context context=null;
	public AsyncViewTask(Context mContext, String mobileinfo) 
	{  

		imageCache = new HashMap<String, SoftReference<Bitmap>>();  

	} 
	public AsyncViewTask() 
	{  

		imageCache = new HashMap<String, SoftReference<Bitmap>>();  

	}
	protected Bitmap doInBackground(View... views) 
	{  
		try
		{ 
			Bitmap bitmap = null;  
			BitmapFactory.Options options=new BitmapFactory.Options();
		    options.inJustDecodeBounds = false;
		    options.inSampleSize = 4;   //width��hight��Ϊԭ����ʮ��һ
			View view = views[0];  
			this.mView = (ImageView)view;  
			if (view.getTag() != null&&view.getTag().toString().length()>0) {
				
				if (imageCache.containsKey(view.getTag())) {  
					SoftReference<Bitmap> cache = imageCache.get(view.getTag().toString());  
					bitmap = cache.get();  
					if (bitmap != null) {
						return bitmap;  
					} 
				}
			 	
				if (URLUtil.isHttpUrl(view.getTag().toString())) 
				{// ���Ϊ�����ַ��������url����ͼƬ  
					URL url = new URL(view.getTag().toString());  
					HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
					conn.setDoInput(true);  
					conn.connect();  
					InputStream stream = conn.getInputStream();  
					Bitmap  bit=BitmapFactory.decodeStream(stream, null, options);
					stream.close();  
					bitmap=Bitmap.createScaledBitmap(bit, 80, 80, true);//预先缩放，避免实
					bit.recycle();
					
				} 
				else
				{// ���Ϊ������ݣ�ֱ�ӽ���  
					//drawable = Drawable.createFromPath(view.getTag().toString());  		
					try {
						Bitmap bit=BitmapFactory.decodeFile(view.getTag().toString(),options);
						bitmap=Bitmap.createScaledBitmap(bit, 80, 80, true);//预先缩放，避免实
						bit.recycle();
						//bitmap=safeDecodeStream(Uri.parse(view.getTag().toString()),78,76);
					} catch (RuntimeException e) {
						// TODO: handle exception
						return null;
					}
				}  
				imageCache.put(view.getTag().toString(), new SoftReference<Bitmap>(bitmap));
				options=null;
				return bitmap;  
			}
			else
			{
				return null;
			}
		}
		catch (Exception e)
		{  
			return null;  
		}    
		
	}  
	protected void onPostExecute(Bitmap bitmap) {  

		if (bitmap != null) {  
			//this.mView.setBackgroundDrawable(drawable); 
			this.mView.setImageBitmap(bitmap);
			this.mView = null;  
		} 
		else
		{
			this.mView=null;
		}
	}  
	/**  
     * A safer decodeStream method  
     * rather than the one of {@link BitmapFactory}  
     * which will be easy to get OutOfMemory Exception  
     * while loading a big image file.  
     *   
     * @param uri  
     * @param width  
     * @param height  
     * @return  
     * @throws FileNotFoundException  
     */  
    protected Bitmap safeDecodeStream(Uri uri, int width, int height)   
    throws FileNotFoundException{   
        int scale = 1;   
        BitmapFactory.Options options = new BitmapFactory.Options();   
        android.content.ContentResolver resolver = this.context.getContentResolver();   
           
        if(width>0 || height>0){   
            // Decode image size without loading all data into memory   
            options.inJustDecodeBounds = true;   
            BitmapFactory.decodeStream(   
                    new BufferedInputStream(resolver.openInputStream(uri), 16*1024),   
                    null,   
                    options);   
               
            int w = options.outWidth;   
            int h = options.outHeight;   
            while (true) {   
                if ((width>0 && w/2 < width)   
                        || (height>0 && h/2 < height)){   
                    break;   
                }   
                w /= 2;   
                h /= 2;   
                scale *= 2;   
            }   
        }   
  
        // Decode with inSampleSize option   
        options.inJustDecodeBounds = false;   
        options.inSampleSize = scale;   
        return BitmapFactory.decodeStream(   
                new BufferedInputStream(resolver.openInputStream(uri), 16*1024),    
                null,    
                options);   
    } 
} 
